/**
 *
 */
package com.ybkj.daijia.server.service.impl;


import com.ybkj.daijia.common.Distance;
import com.ybkj.daijia.server.mapper.UsuallyDriverMapper;
import com.ybkj.daijia.server.model.Driver;
import com.ybkj.daijia.server.model.UsuallyDriver;
import com.ybkj.daijia.server.service.UsuallyDriverService;
import java.text.DecimalFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Shine
 *
 */
@Service
public class DefaultUsuallyDriverService implements UsuallyDriverService {

    @Autowired
    private UsuallyDriverMapper usuallyDriverMapper;


    @Transactional(rollbackFor = Exception.class)
    public UsuallyDriver insertRecord(UsuallyDriver usuallyDriver) {

        usuallyDriverMapper.insertRecord(usuallyDriver);

        return usuallyDriver;
    }

    @Transactional(rollbackFor = Exception.class)
    public void delete(UsuallyDriver usuallyDriver) {

        usuallyDriverMapper.deleteByPrimaryKey(usuallyDriver.getId());

    }

    @Override
    public UsuallyDriver findOne(Long id) {

        return usuallyDriverMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<UsuallyDriver> getUsuallyDriver(final Long memberId) {

        return usuallyDriverMapper.selectByMember(memberId);
    }

    @Override
    public List<UsuallyDriver> getUsuallyDriver(final String phone, Double longitude,
        Double latitude) {

        List<UsuallyDriver> udriverlist = usuallyDriverMapper.selectByMemberPhone(phone);

        if (org.apache.commons.collections.CollectionUtils.isNotEmpty(udriverlist)) {
            for (UsuallyDriver usually : udriverlist) {
                //计算距离
                double distance = Distance
                    .doubleVal(latitude, longitude, usually.getDriver().getLatitude(),
                        usually.getDriver().getLongitude());
                usually.getDriver().setDistance(distance);
                DecimalFormat df = new DecimalFormat("#.00");
                if (distance >= 1) {
                    usually.getDriver().setDistanceStr(df.format(distance) + "公里");
                } else {
                    usually.getDriver().setDistanceStr((long) (distance * 1000) + "米");
                }
            }

            Collections.sort(udriverlist, new Comparator<UsuallyDriver>() {
                public int compare(UsuallyDriver arg0, UsuallyDriver arg1) {
                    return arg1.getDriver().getStarLevel()
                        .compareTo(arg0.getDriver().getStarLevel());
                }
            });

            Collections.sort(udriverlist, new Comparator<UsuallyDriver>() {
                public int compare(UsuallyDriver arg0, UsuallyDriver arg1) {
                    return arg0.getDriver().getDistance().compareTo(arg1.getDriver().getDistance());
                }
            });

            Collections.sort(udriverlist, new Comparator<UsuallyDriver>() {
                public int compare(UsuallyDriver arg0, UsuallyDriver arg1) {
                    if (!arg0.getDriver().getStatus().equals(Driver.DRIVER_OFFLINE)) {
                        return arg0.getDriver().getStatus().compareTo(arg1.getDriver().getStatus());
                    }
                    return 1;
                }
            });

        }

        return udriverlist;
    }

    @Override
    public UsuallyDriver findByDriverAndMember(Long driverId, String phone) {

        return usuallyDriverMapper.selectByDriverAndMember(driverId, phone);
    }

}